package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.ToolUtil;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class SuppevaluaHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String evaluation = MapUtils.getString(data,"evaluation");
        //查询子表供应商编号
        List<Map<String, Object>> bySql = handlerService.findBySql("select suppliercode from gq_vendorlist where evaluation ='" + evaluation + "'");
        if(ToolUtil.isNotEmpty(bySql)){
            for(Map<String, Object> map :bySql){
                String suppliercode =MapUtils.getString(map,"suppliercode");
                if(ToolUtil.isNotEmpty(suppliercode)){
                    handlerService.updateBySql("update gq_account set glgyspj ='"+evaluation+"' where supplierno ='"+suppliercode+"'");
                }
            }

        }
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        List<Map<String, Object>> bySql = handlerService.findBySql("select evaluation from gq_suppevalua where id =" + id);
        String evaluation =MapUtils.getString(bySql.get(0),"evaluation");
        List<Map<String, Object>> bySql1 = handlerService.findBySql("select finalscore from gq_vendorlist where evaluation ='" + evaluation + "'");
        String finalscore =MapUtils.getString(bySql.get(0),"finalscore");
        String suppliercode =MapUtils.getString(bySql.get(0),"suppliercode");
        handlerService.updateBySql("update gq_account set assessment ='"+finalscore+"' where supplierno ='"+suppliercode+"'");
    }
}
